import router from "@/router";
import Layout from "@/layout/index.vue";

// 注意：这个文件是设置动态路由的
// permissions是一个资源的数组
export function activeRouter(permissions) {
    console.log(router.getRoutes())
    let root = {
        path: '/',
        name: 'layout',
        component: Layout, // 修改为 Layout 组件
        children: []
    }
    // 懒加载的问题：https://www.itcan.cn/2020/09/09/dynamic-route/
    permissions.forEach(p => {
        const object = {
            path: p.path,
            name: p.name,
            component: (resolve) => require([`@/views/${p.component}`], resolve)
        }
        root.children.push(object)
    })
    // 动态添加路由信息，重复的路由会被覆盖掉
    // console.log(root)
    router.addRoute(root)
    // console.log(router.getRoutes())
}
